package Chapter02;

public class Person {
        ////////////////////////////////////////
        // 實體變數
        String name;
        int blood;

        int year;
        int month;
        int day;
        
        ////////////////////////////////////////
        // 方法
        void generate() {
                // 產生姓名
                String[] fname = {"曾","林","金","安", "令狐"};
                String[] mname = {"于","志","城","心", ""};
                String[] lname = {"修","玲","武","亞", "沖"};
                
                int findex = (int)(Math.random() * fname.length);
                int mindex = (int)(Math.random() * mname.length);
                int lindex = (int)(Math.random() * lname.length);
                
                name = fname[findex] + mname[mindex] + lname[lindex];
                
                // 計算生日
                calcBirthday();

                // 計算血型
                blood = (int)(Math.random() * 4);       // 0~3
        }
        
        void print() {
                System.out.println("姓名: "+name);
                System.out.println("年齡: "+getAge());
                System.out.printf("生日: %d年%d月%d日\n", year, month, day);
                System.out.printf("生肖: 屬%s的\n", getZodiac());
                switch (blood) {
                case 0:
                        System.out.println("血型: O");
                        break;
                case 1:
                        System.out.println("血型: A");
                        break;
                case 2:
                        System.out.println("血型: B");
                        break;
                case 3:
                        System.out.println("血型: AB");
                        break;
                default:
                        break;
                }
                System.out.println("========================================");
        }

        int getAge() {
                return 2011-year;
        }
        
        String getZodiac() {
                //String[] zodiacTable = {"鼠","牛","虎","兔","龍","蛇","馬","羊","猴","雞","狗","豬"};
                String[] zodiacTable = {"猴","雞","狗","豬","鼠","牛","虎","兔","龍","蛇","馬","羊"};
                int zodiacIndex = year % 12; 
                return zodiacTable[zodiacIndex];
        }
        
        void calcBirthday() {
                year = (int)(Math.random() * 50) + 1950;
                month = (int)(Math.random() * 12) + 1;
                getDayType3();
        }

        void getDayType1() {
                // type 1.
                day = (int)(Math.random() * 31) + 1;
                if (day >= 29 && month == 2) {
                        day = 28;
                }
                if (day == 31 && (
                        month == 4 || month == 6 || month == 9 || month == 11 
                        )) {
                        day = 30;
                }
        }
        
        void getDayType2() {
                // type 2.
                int daysOfMonth = 0;
                switch(month) {
                case 4:
                case 6:
                case 9:
                case 11:
                        daysOfMonth = 30;
                        break;
                case 2:
                        daysOfMonth = 28;
                        break;
                default:
                        daysOfMonth = 31;
                        break;
                }
                day = (int)(Math.random() * daysOfMonth) + 1;           
        }
        
        void getDayType3() {
                // type 3.
                int[] daysOfMonthArray = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                int daysOfMonth = daysOfMonthArray[month-1];
                day = (int)(Math.random() * daysOfMonth) + 1;
        }
}
